''''''''''''' Main Start.''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' var args1=WScript.Arguments.Item(0)
' var args2=WScript.Arguments.Item(1)

workDriver = "E"
workDir = "\WinVhds"
vhdBase = "work0_offc11.vhdx"
tarName = "work.tar"
vhdA = "work11A.vhdx"
vhdB = "work11B.vhdx"
tmpSciptFile = "vhdbuild.script"

dim fso
set fso = initFSO()

absDir = workDriver & ":" & workDir
absVhdBase = joinPath(absDir, vhdBase)
absVhdA = joinPath(absDir, vhdA)
absVhdB = joinPath(absDir, vhdB)

delFile fso, absVhdA
delFile fso, absVhdB
delFile fso, joinPath(absDir, tarName)

scriptFile = joinPath(absDir, tmpSciptFile)
createChildVhd fso, scriptFile, absVhdA, absVhdBase
createChildVhd fso, scriptFile, absVhdB, absVhdBase

createTar absDir, tarName, vhdA, vhdB

set fso = Nothing
'''''''''''''''Main End.''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function initFSO()
    set fso = CreateObject("Scripting.FileSystemObject")
    set initFSO = fso
End Function

Function joinPath(dir, filename)
    joinPath = dir & "\" & filename
End Function

Function delFile(fso, filename)
    res = False
    On Error Resume Next
    fso.DeleteFile filename, True
    If Err.Number = 0 Then
        res = True
    End If

    delFile = res
End Function

Sub createChildVhd(fso, scriptFileName, childvhd, parentvhd)
    set fl = fso.OpenTextFile(scriptFileName, 2, True) 'If not exist, create file.
    fl.WriteLine "create vdisk file=" & childvhd & " parent=" & parentvhd
    fl.Close

    runCmd "diskpart /s " & scriptFileName
    delFile fso, scriptFileName
End Sub

' Sub createChildVhd(childvhd, parentvhd)
'     set wssh = CreateObject("WScript.Shell")
'     Set exec = wssh.Exec("diskpart.exe")
'     exec.StdIn.WriteLine "create vdisk file=" & childvhd & " parent=" & parentvhd
'     set exec = Nothing
'     set wssh = Nothing
' End Sub

Sub createTar(absDir, tarName, vhdA, vhdB)
    ' tar -zcvf work.tar work11A.vhdx work11B.vhdx
    runCmdAtDir "tar -zcvf " & tarName & " " & vhdA & " " & vhdB, absDir
End Sub

Sub runCmd(strCmd)
    set wssh = CreateObject("wscript.shell")
    wssh.run strCmd, vbhide, True
    set wssh = Nothing
End Sub

Sub runCmdAtDir(strCmd, dir)
    set wssh = CreateObject("wscript.shell")
    wssh.CurrentDirectory = absDir
    wssh.run strCmd, vbhide, True
    set wssh = Nothing
End Sub